<template>
  <div class="toolbar">
    <div class="start">
      <slot></slot>
    </div>

    <template v-if="$slots.center">
      <div class="center">
        <slot name="center"></slot>
      </div>
    </template>

    <template v-else>
      <div class="center">
        <el-pagination v-model:current-page="tableModel.pageNum" v-model:page-size="tableModel.pageSize"
          :layout="tableModel.layout" :page-sizes="tableModel.pageSizes" :pager-count="tableModel.pagerCount"
          :total="tableModel.total" @size-change="pageSizeChange" @current-change="pageNumChange" small />
      </div>
    </template>

    <template v-if="$slots.end">
      <div class="end">
        <slot name="end"></slot>
      </div>
    </template>

    <template v-else>
      <div class="end">
        <el-tooltip content="刷新表格" placement="bottom">
          <el-button @click="fetchTableData" icon="Refresh" circle size="small" />
        </el-tooltip>

        <el-popover placement="bottom" title="列设置" :width="500" trigger="click" :hide-after="0">
          <template #reference>
            <el-button icon="SetUp" circle size="small" />
          </template>

          <ColumnSetting :ref="options.table.columnSettingRef" :columns="tableModel.columnSetting"
            @save="saveColumnSetting" @resetColumn="resetColumnSetting" />
        </el-popover>
        <el-popover placement="bottom" title="表格设置" :width="400" trigger="click" :hide-after="0">
          <template #reference>
            <el-button icon="Setting" circle size="small" />
          </template>
          <el-form label-width="80px" label-position="left">
            <el-form-item label="表格尺寸">
              <el-radio-group v-model="tableModel.size" size="small">
                <el-radio-button label="large">大</el-radio-button>
                <el-radio-button label="default">正常</el-radio-button>
                <el-radio-button label="small">小</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="样式">
              <el-checkbox v-model="tableModel.border" label="纵向边框" />
              <el-checkbox v-model="tableModel.stripe" label="斑马纹" />
              <el-checkbox v-model="tableModel.index" label="行号" />
            </el-form-item>
          </el-form>
        </el-popover>
      </div>
    </template>
  </div>
</template>

<script setup name="Toolbar">
const props = defineProps({
  tableModel: {
    type: Object,
    required: false,
    default: {
      pageNum: undefined,
      pageSize: undefined,
      pageSizes: undefined,
      pagerCount: undefined,
      total: undefined,
      layout: undefined,
      size: undefined,
      border: undefined,
      stripe: undefined,
      index: undefined
    }
  },
  options: {
    type: Object,
    required: false,
    default: {
      table: {
        columnSettingRef: undefined
      }
    }
  }
})

const { proxy } = getCurrentInstance()
const emit = defineEmits()

function pageSizeChange() {
  emit('pageSizeChange', proxy)
}

function pageNumChange(newPageNum) {
  emit('pageNumChange', newPageNum)
}

function fetchTableData() {
  emit('fetchTableData', proxy)
}

function saveColumnSetting(val) {
  // console.log('toolbar columnSetting save', val)
  emit('saveColumnSetting', val)
}

function resetColumnSetting() {
  emit('resetColumnSetting', proxy)
}
</script>

<style lang="scss" scoped>
.toolbar {
  display: flex;
  height: 40px;
  border: 1px solid var(--el-border-color);

  box-shadow: 0 -1px 2px #ebeef5, 0 2px 2px #ebeef5;

  align-items: center;
  justify-content: space-between;
  margin-bottom: 1px;
  background-color: #ffffff;

  .start {
    display: flex;
  }

  .center {
    //margin-left: 5px;
  }

  .end {
    margin-right: 5px;
  }
}
</style>
